﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NapCardOnline.Models;
using System.Web.Security;

namespace NapCardOnline.ViewModel
{
    public class AccountViewModels
    {
        NapCardOnlineEntities DBContext;
        public AccountViewModels()
        {
            DBContext = new NapCardOnlineEntities();
        }

        /// <summary>
        /// Validates the user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="passWord">The pass word.</param>
        /// <returns>True or false</returns>
        public User ValidateUser(string userName, string passWord)
        {
            passWord = EncryptPass(passWord);
            var data = DBContext.Users.Where(m => m.UserName == userName && m.Password == passWord && m.IsActive == true).ToList();
            if (data.Count > 0)
            {
                return data[0];
            }
            else return null;
        }


        /// <summary>
        /// Creates the user.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>Membership create status</returns>
        public MembershipCreateStatus CreateUser(RegisterModel model)
        {
            MembershipCreateStatus status = new MembershipCreateStatus();
            User user = new User();
            user.UserNo = MvcUtil.ObjectUtil.GenerateNumber("User", DBContext.Users, u => u.UserID);
            user.UserName = model.UserName;
            user.Phone = model.Phone;
            user.UserFullName = model.UserNameFull;
            user.Email = model.Email;
            user.UserLevel = (int) Enum.UserLevel.Level2;
            user.FK_CommissionLevelID = (int)Enum.CommissionLevel.Level2;
            user.UserBalance = 0;
            if (DBContext.Users.FirstOrDefault(u => u.Email == user.Email) == null)
            {
                user.Password = EncryptPass(model.Password);
                string confirmPassWord = EncryptPass(model.ConfirmPassword);
                user.IsActive = true;
                if (confirmPassWord == user.Password)
                {
                    DBContext.Users.AddObject(user);
                    int id = DBContext.SaveChanges();
                    if (id > 0)
                        status = MembershipCreateStatus.Success;
                }
            }
            else
            {
                status = MembershipCreateStatus.DuplicateEmail;

            }
            return status;
        }

        /// <summary>
        /// Encrypts the pass.
        /// </summary>
        /// <param name="passWord">The pass word.</param>
        /// <returns>Password encrypt</returns>
        public string EncryptPass(string passWord)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(passWord.Trim(), "SHA1");
        }

        /// <summary>
        /// Checks the name of the exists user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns>True if not exists this user name else false</returns>
        public bool CheckExistsUserName(string userName)
        {

            int countUser = DBContext.Users.Count(x => x.UserName.ToLower() == userName.ToLower().Trim());
            if (countUser < 1)
            {
                return false;
            }
            else
                return true;
        }
    }
}